<?php

namespace app\index\controller;

use app\common\service\TerminalService;
use think\Request;
use app\common\utils\ResultVOUtil;
use app\common\enums\ResultEnum;
use app\common\model\Cars;
use app\common\model\CarsInfo;
use app\common\model\Terminal as terminalModel;
class Terminal extends Base
{
    protected $terminalService;
    
    public function __construct(TerminalService $terminalService)
    {
        parent::initialize();
        $this->terminalService = $terminalService;
    }

    public function versionAdd(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->terminalService->versionAdd($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function versionList(){
        $data = $this->terminalService->versionList();
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }
    }
    
    public function versionDel(int $id){
        if(!empty($id)){
            $data = $this->terminalService->versionDel($id);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }else{
            return ResultVOUtil::error(ResultEnum::PARAM_ERROR);
        }
    }
    
    public function index(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $where = [];
            if(!empty($param['supplier_no'])){
                $where[] = ['supplier_no','=',$param['supplier_no']];
            }
            
            if(!empty($param['status'])){
                if($param['status'] == 1){
                    $where[] = ['status','=',$param['status']];
                }else{
                    $where[] = ['status','=',0];
                } 
            }
            
            if ( !empty($param['search']) ) {
                $where[] = ['sim_id|cardcode|equipment_id','like','%'.$param['search'].'%'];
            }
            
            $data = $this->terminalService->index($where, $param['length'], $param['page']);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }else{
            return ResultVOUtil::error(ResultEnum::PARAM_ERROR);
        }
    }
    
    public function upload(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->terminalService->uploadFile($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function list($id = 0){
        $data = $this->terminalService->list($id);

        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }       
    
    }
    
    public function edit(Request $request){
        $param = $request->param();
        if (!empty($param)) {
            $data = $this->terminalService->edit($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::UPDATE_ERROR);
            }   
        }
    }
    
    public function user(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $where = [];
            
            if(!empty($param['type'])){
                $where[] = ['type','=',$param['type']];
            }
            
            if(!empty($param['user_id'])){
                $where[] = ['user_id','=',$param['user_id']];
            }
            
            if ( !empty($param['search']) ) {
                $carId = Cars::where('car_sn|sim_id|cardcode|equipment_id','like','%'.$param['search'].'%')->column('car_id');
                $where[] = ['car_id','in',$carId];
            }
            
            $result = $this->terminalService->user($where, $param['length'], $param['page']);
            
            if($result){
                return ResultVOUtil::success($result);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            } 
        }
    }
    
    public function userList(){
        $data = $this->terminalService->userList();
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }  
    }
    
    public function userEdit(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->terminalService->userEdit($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::UPDATE_ERROR);
            }  
        }
    }
    
    public function log(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->terminalService->installLog($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::ERROR);
            } 
        }
    }
    
    public function installDel($id = 0){
        $data = $this->terminalService->userDel($id);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        } 
    }
    
    public function upgrade(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->terminalService->upgrade($param);
            if($data){
                return ResultVOUtil::success($data);
            }elseif ($data == 0) {
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::ERROR);
            } 
        }
    }
    
    public function exchange(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->terminalService->exchange($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::ERROR);
            } 
        }
    }
    
    public function getVersion(String $version=''){
        $data = $this->terminalService->searchVersion($version);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        } 
    }
    
    public function machineList(Request $request){
        $data = $request->param();
        $where = [];
        if(!empty($data)){
            if ( !empty($data['search']) ) {
                $where[] = ['car_sn|car_unique_id','like','%'.$data['search'].'%'];
            }
            
            if ( !empty($data['agency_id']) ) {
                $where[] = ['agency_id','=',$data['agency_id']];
            }
            
            if ( !empty($data['brand_id']) ) {
                $where[] = ['brand_id','=',$data['brand_id']];
            }
            if ( !empty($data['version']) ) {
                $where[] = ['car_unique_id','in',CarsInfo::where('machine_version',$data['version'])->column('car_unique_id')];
                
            }
            
            if ( !empty($data['supplier_no']) ) {
                $where[] = ['supplier_no','=',$data['supplier_no']];
            }else{
                $where[] = ['supplier_no','in','1376920002,1376920003'];
            }
            
            if ( !empty($data['sim_card']) ) {
                $where[] = ['sim_card','=',$data['sim_card']];
            }
            $result = $this->terminalService->machineList($where, $data['length'], $data['page']);
            if($result){
                return ResultVOUtil::success($result);
            }else{
                return ResultVOUtil::error(ResultEnum::ERROR);
            } 
        }
    }
    
    public function register($id = 0){
        $data = $this->terminalService->register($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        } 
    }
    
    public function queryDetails($id=0){
        $data = $this->terminalService->queryDeviceDetails($id);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        } 
    }
    
    public function deviceDel($id= 0){
        $data = $this->terminalService->deviceDel($id);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        } 
    }
    
}
